require('./index.css');
require('page/common/nav/index.js');
require('page/common/header/index.js');
var _mm = require('util/mm.js');
var templateIndex = require('./index.string');
var _pay = require('service/payment-service.js');

var page = {
  data: {
    orderNo: _mm.getUrlParam('orderNumber')
  },
  init: function() {
    this.onLoad();
  },
  onLoad: function() {
    this.loadPayment();
  },
  loadPayment: function() {
    var _this = this,
        payHtml = '',
        $pageWrap = $('.page-wrap');
    $pageWrap.html('<div class="loading"></div>')
    _pay.pay(this.data.orderNo, function(res) {
      payHtml = _mm.renderHtml(templateIndex, res);
      $pageWrap.html(payHtml);
      // 监听订单状态
      _this.listenOrderStatus();
    }, function(errMsg) {
      $pageWrap.html('<p class="err-tip">'+ errMsg +'</p>');
    })
  },
  listenOrderStatus: function() {
    var _this = this;
    this.paymentTimer = window.setInterval(function() {
      _pay.queryStatus(_this.data.orderNo, function(res) {
        if(res == true) {
          window.location.href 
          = './result.html?type=payment&orderNumber=' + _this.data.orderNo;
        }
      }, function(errMsg) {
        $('.page-wrap').html('<p class="err-tip">'+ errMsg +'</p>');
      })
    }, 5e3);
  }
};

$(function() {
  page.init();
})